using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace SinceTimes.NetWork
{
    [System.Serializable]
    public class BuffMSG : IPeerMessage
    {
        static short SIZE_Length_Code = 4;
        static short SIZE_Type_Code = 2;
        static int SIZE_HEAD => SIZE_Length_Code + SIZE_Type_Code; 
        public bool Empty { get; set; }

        public uint Length { get;internal set; }
        public string Text;

        public byte[] _Buff;

        //public T Data;

        public BuffMSG()
        { 
            Empty = true;
        }
        public BuffMSG(int type)
        {
            Empty = true;
            MessageType =(ushort) type;
        }
        public BuffMSG(int type,string text)
        {
            Empty = true;
            MessageType = (ushort)type;
            Text = text;
        }
        // public TianwangMSG( )
        public BuffMSG(byte[] _bytes, uint _offset, uint _size =0)
        {
            Empty = true;
            Decode(_bytes, _offset, _size);
        }
        public  void Decode(byte[] _bytes, uint _offset, uint _size )
        {
            if (_bytes == null || _bytes.Length < SIZE_HEAD)
                return;

            Length = Net.BYTE_USHORT(_bytes, _offset+0);  

            MessageType =Net.BYTE_USHORT(_bytes, _offset+4);
            try
            {
                _Buff = new byte[_size>0? Math.Max(0,  _size- SIZE_HEAD) : _bytes.Length - SIZE_HEAD];
                Array.Copy(_bytes, _offset+SIZE_HEAD, _Buff, 0, _Buff.Length);

                Text = System.Text.Encoding.UTF8.GetString(_Buff);
                            }
            catch (Exception e)
            {
                if (Net.SHOWERROR) 
                    Debug.LogError($"[BuffPeer.Decode]:Error:{e.Message}");
            }


            Empty = false;
        }

        public uint Serial { get; set; }
        public ushort MessageType { get; set; }

        public int MessageIndex { get; internal set; }

        public  byte[] Encode()
        {
            if (string.IsNullOrEmpty(Text))
                Text = "";
            byte[] _datas = System.Text.Encoding.UTF8.GetBytes(Text);

            Length = (uint)(SIZE_HEAD+ _datas.Length);

            byte[] _buff = new byte[Length];// + _datas.Length];

            // 总长度
            Array.Copy(Net.UINT_BYTES (Length), 0, _buff, 0, 4);

            // 消息类型
            Array.Copy(Net.USHORT_BYTES(MessageType), 0, _buff, 4, 2);

            Array.Copy(_datas, 0, _buff, 6, _datas.Length);

            return _buff;
        }
        public T DataAs<T>()
        {
            return default(T);// (T)_Buff;
        }
        public override string ToString()
        {
            if(!string.IsNullOrEmpty(Text))
                return $"Len={Length};Type={MessageType} \n--------------- \n{Text }\n---------------";
            else
                return $"Len={Length};Type={MessageType}";
        }

    }
    public class BuffPeer : Peer<BuffMSG>
    {


        protected override BuffMSG _DoDecode(byte[] _buffer,  uint _offset, uint _size)
        {
            BuffMSG msg = _size > 0 ? new BuffMSG(_buffer, _size) : null;

            return msg;
        }

    }
}

